package com.ytktt.study.week11.task9;

import com.ytktt.study.week11.task9.constant.RedisConstant;
import io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.RedisPubSubListener;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.async.RedisPubSubAsyncCommands;
import lombok.extern.slf4j.Slf4j;

/**
 * sub 应用
 *
 * @author ytk
 */
@Slf4j
public class SubApplication {

    public static void main(String[] args) throws InterruptedException {
        RedisClient redisClient = RedisClient.create(RedisConstant.REDIS_SERVER);
        StatefulRedisPubSubConnection<String, String> stringStringStatefulRedisPubSubConnection = redisClient.connectPubSub();
        stringStringStatefulRedisPubSubConnection.addListener(new RedisPubSubListener<String, String>() {
            @Override
            public void message(String channel, String message) {
                log.info("message method channel: {}, message: {}", channel, message);
            }

            @Override
            public void message(String pattern, String channel, String message) {
                log.info("message method pattern: {}, channel: {}, message: {}", pattern, channel, message);
            }

            @Override
            public void subscribed(String channel, long count) {
                log.info("message subscribed channel: {}, count: {}", channel, count);
            }

            @Override
            public void psubscribed(String pattern, long count) {
                log.info("message psubscribed pattern: {}, count: {}", pattern, count);
            }

            @Override
            public void unsubscribed(String channel, long count) {
                log.info("message unsubscribed channel: {}, count: {}", channel, count);
            }

            @Override
            public void punsubscribed(String pattern, long count) {
                log.info("message punsubscribed pattern: {}, count: {}", pattern, count);
            }
        });
        RedisPubSubAsyncCommands<String, String> async = stringStringStatefulRedisPubSubConnection.async();
        async.subscribe(RedisConstant.TOPIC);

        Thread.sleep(100_000L);
        stringStringStatefulRedisPubSubConnection.close();
        redisClient.shutdown();
    }
}
